package com.wenwei.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wenwei.bean.AllCouponBean;
import com.wenwei.bean.UserCouponBean;
import com.wenwei.parking.Constant;
import com.wenwei.parking.R;
import com.wenwei.utils.RogerDateUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/22.
 */
public class CouponAdapter extends RecyclerView.Adapter<CouponAdapter.MyViewHolder> {

    private int mItemType;//默认隐藏使用优惠券
    private int mState;

    public interface CallBack{
        void onItemClick(int couponId);
        void onUseCoupon(UserCouponBean bean);
    }

    private CallBack mCallBack ;

    public void setCallBack(CallBack callBack){
        this.mCallBack = callBack;
    }

    public CouponAdapter(int mType,int state) {
        this.mItemType = mType;
        this.mState = state;
    }

    private ArrayList<UserCouponBean> mUserList;
    public void setmUserList(ArrayList<UserCouponBean> list) {
        this.mUserList = list;
    }

    private ArrayList<AllCouponBean> mALlList;
    public void setmALlList(ArrayList<AllCouponBean> list){
        this.mALlList = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_coupon, null));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        if(mItemType == Constant.COUPON_USER){
            holder.quanPriceTv.setText(mUserList.get(position).getCoupon().getDiscount()+"");
            holder.quanFPriceTv.setText("满"+mUserList.get(position).getCoupon().getFullPrice()+"元使用");
            holder.quanNameTv.setText(mUserList.get(position).getCoupon().getCouponName());
            holder.quanEndTime.setText("有效期至："+ RogerDateUtils.getDateToString(mUserList.get(position).getCoupon().getActiveTime()));
            if(mState == 1){
                holder.couponUsed.setVisibility(View.VISIBLE);
                holder.couponUsed.setText("立即使用");
                holder.couponUsed.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mCallBack.onUseCoupon(mUserList.get(position));
                    }
                });
            }else {
                holder.couponUsed.setVisibility(View.GONE);
            }
        }else {
            holder.quanPriceTv.setText(mALlList.get(position).getDiscount()+"");
            holder.quanFPriceTv.setText("满"+mALlList.get(position).getFullPrice()+"元使用");
            holder.quanNameTv.setText(mALlList.get(position).getCouponName());
            holder.quanEndTime.setText("有效期至："+ RogerDateUtils.getDateToString(mALlList.get(position).getActiveTime()));
            if(mALlList.get(position).getIsUserHave() == 1){
                holder.couponUsed.setBackgroundResource(R.drawable.button_state_1);
                holder.couponUsed.setTextColor(Color.parseColor("#838181"));
                holder.couponUsed.setText("已领取");
            }else {
                holder.couponUsed.setBackgroundResource(R.drawable.button_state_2);
                holder.couponUsed.setTextColor(Color.parseColor("#ffffff"));
                holder.couponUsed.setText("立即领取");
                holder.couponUsed.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(mCallBack!=null){
                            mCallBack.onItemClick(mALlList.get(position).getId());
                        }
                    }
                });
            }

        }
    }

    @Override
    public int getItemCount() {
        if (mItemType == Constant.COUPON_USER) {
            return mUserList == null ? 0 : mUserList.size();
        } else {
            return mALlList == null ? 0 : mALlList.size();
        }

    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.quan_price_tv)
        TextView quanPriceTv;
        @Bind(R.id.quan_f_price_tv)
        TextView quanFPriceTv;
        @Bind(R.id.quan_name_tv)
        TextView quanNameTv;
        @Bind(R.id.quan_end_time)
        TextView quanEndTime;
        @Bind(R.id.coupon_used)
        TextView couponUsed;

        public MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


}
